package cxm.proj.xtalk.smack;

import android.content.Intent;
import cxm.lib.util.MyApplication;
import cxm.lib.util.MyLog;

public class UserManager {
	private static UserManager instance = null;
	
	private String userName = null;
	private String password = null;;
	private User user = null;
	ConnectTaskResultHandle connectHandle = null;
	Object handleTag = null;
	
	private UserManager() {
	}
	
	public void setLoginAccount(String username, String password) {
		MyLog.Log(MyLog.Level.Debug, "Set login account: " + username);
		/*
		StackTraceElement []elements = Thread.currentThread().getStackTrace();
		for (int i = 0; i < elements.length; i++)
			MyLog.Log(MyLog.Level.Debug, "Call stack " + i + " :" + elements[i].getMethodName());
		*/

		if (null != this.user)
			throw new IllegalStateException("Already exist");
		
		this.userName = username;
		this.password = password;
		this.user = new User(this.userName, this.password);
	}
	
	// has user setup
	public boolean hasUser() {
		return null != this.user;
	}
	
	public User getUser() {
		if (null == this.user)
			throw new IllegalStateException("User not login");
		
		return this.user;
	}
	
	public String getLoginName() {
		return this.userName;
	}
	
	public String getLoginPassword() {
		return this.password;
	}
	
	// if taskResultHandle is null then sync login
	// otherwise async login
	public void login(ConnectTaskResultHandle taskResultHandle,
			Object taskResultHandleTag) throws UserException {
		if (!this.user.isLogout())
			throw new UserException("Not logout");

		if (null != taskResultHandle) {
			this.connectHandle = taskResultHandle;
			this.handleTag = taskResultHandleTag;
			this.user.login(new ConnectTaskResultHandle() {
				@Override
				public void taskResultHandle(int result, Object tag) {
					// start online service for keeping online
					// and listening incoming calls
					if (0 == result) {
						Intent intent = new Intent(MyApplication.context, OnlineService.class);
						MyApplication.context.startService(intent);
					}

					// on custom callback
					connectHandle.taskResultHandle(result, handleTag);
				}
			}, null);
		} else {
			if (!this.user.login())
				throw new UserException("Cannot login");
		}
	}
	
	public static void Release() {
		if (null == instance)
			return;

		if (null != instance.user)
			try {
				MyLog.Log(MyLog.Level.Debug, "Release account: " + instance.user.getName());
				if (instance.user.isLogin())
					instance.user.logout(null, null);
				instance.connectHandle = null;
				instance.handleTag = null;
			} catch (Exception e) {
				MyLog.Log(MyLog.Level.Error, "Error when logout: " + e);
			}

		instance = null;
	}
	
	public static String getUserNameByJid(String jid) {
		return jid.split("@")[0];
	}

	public static String getJidByUserName(String userName) {
		return userName + "@" + ConnectManager.getInstance().getServiceName();
	}
	
	public static UserManager getInstance() {
		if (null == instance)
			instance = new UserManager();
		return instance;
	}
}
